package com.eagle.epay.controller;


import com.eagle.epay.common.ResultVO;
import com.eagle.epay.constant.EPayConstant;
import com.eagle.epay.entity.User;
import com.eagle.epay.service.IOrderService;
import com.eagle.epay.util.ResultVOUtil;
import com.eagle.epay.vo.OrderVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author eagle
 * @since 2019-12-21
 */
@RestController
@RequestMapping("/order")
public class OrderController {
	@Autowired
	IOrderService orderService;

	@GetMapping("/create")
	public ResultVO<PageInfo> list(@RequestParam("shippingId")  Integer shippingId,
								   HttpSession session) throws Exception{
		User curUser =(User) session.getAttribute(EPayConstant.CURUSER);
		OrderVo orderVo = orderService.create(curUser.getId(), shippingId);
		return ResultVOUtil.success(orderVo);
	}

	@GetMapping("/list")
	public ResultVO<PageInfo> list(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
								   @RequestParam(required = false, defaultValue = "10") Integer pageSize,
								   HttpSession session) {
		User curUser =(User) session.getAttribute(EPayConstant.CURUSER);
		PageInfo list = orderService.list(curUser.getId(), pageNum, pageSize);
		return ResultVOUtil.success(list);
	}

	@GetMapping("/detail")
	public ResultVO<PageInfo> detail(@RequestParam String orderId,
								   HttpSession session) {
		User curUser =(User) session.getAttribute(EPayConstant.CURUSER);
		OrderVo detail = orderService.detail(curUser.getId(), orderId);
		return ResultVOUtil.success(detail);
	}

}

